<template>
<div id="discuss">
  <el-form ref="form" :model="discuss" label-width="80px" v-if="currentUserId">
    <el-form-item label="标题">
      <el-input v-model="discuss.title"></el-input>
    </el-form-item>
    <el-form-item label="内容">
      <el-input type="textarea" v-model="discuss.content"></el-input>
    </el-form-item>
    <el-form-item style="text-align: right;">
      <el-button type="primary" @click="save">发布</el-button>
      <el-button @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
  <el-collapse v-model="activeNames">
    <el-collapse-item :name="item.id" v-for="item in discussList" :key="item.id">
      <template slot="title">
        {{item.title}}
        <span style="margin-left: 40px;" v-if="item.createrName">
          <i class="el-icon-user-solid"></i> 
          {{item.createrName}}
        </span>
        <span style="margin-left: 20px;">
          <i class="el-icon-time"></i> 
          {{item.createTime | timestamp('yyyy-MM-dd hh:mm')}}
        </span>
      </template>
      <div>{{item.content}}</div>
    </el-collapse-item>
  </el-collapse>
</div>
</template>

<script>
import http from '@/components/service/http.js'

export default {
  name: 'discuss',
  data() {
    return {
      currentUserId: '',
      activeNames: [],
      discuss: {
        title: '',
        content: ''
      },
      discussList: []
    }
  },
  created() {
    this.currentUserId = this.$store.state.userId
    this.getList();
  },
  methods: {
    getList: function () {
      var This = this;
      http.get('/shs/discuss/getList')
        .then(function (response) {
          This.discussList = response.data;
        })
    },
    save() {
      var This = this;
      this.$refs['form'].validate((valid) => {
        if (valid) {
          http.post('/shs/discuss/save', This.discuss)
            .then(function () {
              This.$message({
                showClose: false,
                message: "发布成功",
                type: "success",
                duration: 3000,
                center: true
              });
              This.getList();
            })
        } else {
          return false;
        }
      });
    },
    cancel: function () {
      this.discuss = {
        title: '',
        content: ''
      }
    }
  }
}
</script>

<style>
#discuss {
  width: 100%;
}

#discuss .el-collapse-item__wrap {
  text-align: left;
  padding: 10px;
  background: #eee;
  border-radius: 5px;
}
</style>
